package com.atguigu.crowd.funding.service.impl;

import com.atguigu.crowd.funding.entity.Role;
import com.atguigu.crowd.funding.entity.RoleExample;
import com.atguigu.crowd.funding.mapper.RoleMapper;
import com.atguigu.crowd.funding.service.api.RoleService;
import com.atguigu.crowd.funding.util.CrowdFundingUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hftang
 * @date 2019-10-28 10:04
 * @desc
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public PageInfo<Role> queryForKeywordWithPage(Integer pageNum, Integer pageSize, String keyword) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);

        List<Role> roles = this.roleMapper.selectForKeywordSearch(keyword);


        return new PageInfo<>(roles);
    }

    @Override
    public List<Role> queryRoleByIds(List<Integer> roleIds) {

        RoleExample example = new RoleExample();
        example.createCriteria().andIdIn(roleIds);
        List<Role> roleList = this.roleMapper.selectByExample(example);
        return roleList;
    }

    @Override
    public void batchRemove(List<Integer> roleIds) {

        RoleExample example = new RoleExample();
        example.createCriteria().andIdIn(roleIds);
        this.roleMapper.deleteByExample(example);
    }

    @Override
    public void saveRole(String role) {
        this.roleMapper.insert(new Role(null, role));
    }

    @Override
    public void updateRole(Role role) {
        System.out.println(role);
        this.roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public List<Role> getAssignedRoleList(Integer adminId) {

        List<Role> assignedRoleList = this.roleMapper.getAssignedRoleList(adminId);

        return assignedRoleList;
    }

    @Override
    public List<Role> getUnAssignedRoleList(Integer adminId) {

        List<Role> assignedRoleList = this.roleMapper.getUnAssignedRoleList(adminId);
        return assignedRoleList;
    }

    @Override
    public void updateRelationship(Integer adminId, List<Integer> roleIdList) {

        //先全部的删除
        this.roleMapper.deleteOldAdminRelationship(adminId);
        //再重现插入
        if (CrowdFundingUtils.collectionEffective(roleIdList)) {
            this.roleMapper.insertNewAdminRelationship(adminId, roleIdList);
        }


    }
}
